import { APollingTask, CacheData } from "../../PollingStation";

export class CacheDataResource<T extends ICacheResourceTask=ICacheResourceTask> extends CacheData<T>
{
    ResourceRecord:Record<ResourceConstant,T[]>={} as any;
    constructor(data:T[])
    {
        super(data);
        this.ResourceRecord = _.groupBy(data,e=>e.resourceType) as Record<ResourceConstant,T[]>;
    }
    getResource(type:ResourceConstant)
    {
        return this.ResourceRecord[type]
        // if(this.ResourceRecord[type])
        // {
        //     return this.ResourceRecord[type]
        // }
        // return undefined
    }
}

export class CacheResourceTask<T = CacheDataResource<ICacheResourceTask>> extends APollingTask<T>
{
    // roomName!:string;

    Update(...arg:any[]): void
    {
        // this.roomName = arg[0];
        const room = Game.rooms[this.code];
        if(room)
        {
            const depos = room.find(FIND_DROPPED_RESOURCES);
            const cachedata:ICacheResourceTask[]  = _.map(depos,item=>{
                const ret:ICacheResourceTask= {
                    id:item.id as string,
                    pos:item.pos,
                    resourceType:item.resourceType,
                    amount:item.amount,
                } as ICacheResourceTask;
                return ret;
            });
            this.value = new CacheDataResource<ICacheResourceTask>(cachedata) as unknown as  T;
            this.lastupdate =Game.time + 20;
        }
    }

}
